package cn.bdqn.service.course;

import cn.bdqn.dao.course.CourseTypeMapper;
import cn.bdqn.pojo.course.CourseTag;
import cn.bdqn.pojo.course.CourseType;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.List;

@Service
public class CourseTypeService {
    @Autowired
    private CourseTypeMapper courseTypeMapper;
    //根据课程id查询课程标签
    public List<CourseType> selectCourseTypeByCourseId(Integer courseId){return courseTypeMapper.selectList(new QueryWrapper<CourseType>().eq("course_id",courseId));}
    //添加课程标签
    public Integer insertCourseType(CourseType courseType){
        return courseTypeMapper.insert(courseType);
    }

    //添加课课程类型集合
    public boolean insertCourseTypeList(List<CourseType> courseTypes){
        boolean flag = true;
        for (CourseType courseType : courseTypes) {
            int insert = courseTypeMapper.insert(courseType);
            if (insert <= 0) {
                flag = false;
                //设置手动回滚
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                break;
            }
        }
        return flag;
    }

    //根据课程id删除课程标签
    public Integer deleteCourseTypeByCourseId(Integer courseId){
        //检查是否存在原有标签
        List<CourseType> courseTypes = courseTypeMapper.selectList(new QueryWrapper<CourseType>().eq("course_id", courseId));
        if(null == courseTypes || courseTypes.size() == 0){
            return 1;
        }else {
            return courseTypeMapper.delete(new QueryWrapper<CourseType>().eq("course_id",courseId));
        }
    }

    //删除课程标签
    public Integer deleteCourseTypeById(Integer courseTypeId){
        return courseTypeMapper.delete(new QueryWrapper<CourseType>().eq("course_type_id",courseTypeId));
    }

}
